var gulp = require('gulp'),
	jade = require('gulp-jade'), //jade模版
	csso = require('gulp-csso'), //剔除多余css
    less = require('gulp-less'), //编译sass
	minifycss = require('gulp-minify-css'), //压缩css
	spritesmith = require("gulp-spritesmith"), //css雪碧图
	autoprefixer = require('gulp-autoprefixer'), //浏览器前缀
	tinypng = require('gulp-tinypng-compress'), //压缩图片
	uglify = require('gulp-uglify'), //js压缩
	concat = require('gulp-concat'), //文件合并
	notify = require('gulp-notify'), //通知消息
	rename = require('gulp-rename'); //重命名文件
var util= require( 'gulp-util' );
var header = require('gulp-header');

var Version='1.0.0';
var buildDate = util.date(Date.now(), 'isoDate')+" "+util.date(Date.now(), 'isoTime');
var banner = '/*\n * Version:'+Version+'\n * Author:www.junr.me \n * Updated:'+buildDate+'\n*/\n';


/* 设置路径 */
var path = {
		 css : "public/css",
		  js : "public/js",
		less : "public/less",
		 img : "public/images",
		 dist: "dist"
	}

//编译sass文件
gulp.task('sass', function () {
	return gulp.src(path.less + '/*.less')
		.pipe(less()) //expended(展开)  compact(紧凑)  compressed(压缩)  nested(嵌套)
		.pipe(autoprefixer({
			browsers: ['last 20 versions', 'safari 5', 'opera 12.1', 'ios 6', 'android 4']
		}))
		.on('error', function (err) {
			console.error('错误!', err.message);
		})
		.pipe(gulp.dest(path.css))
		.pipe(minifycss())			  //执行压缩
		.pipe(header(banner))
		.pipe(gulp.dest(path.dist+"/css/"))
});

//压缩合并js
gulp.task('js', function () {
	return gulp.src(path.js + '/*.js')
		//.pipe(rename({suffix: '.min'}))
		.pipe(uglify()) //js压缩
		.pipe(header(banner))
		.pipe(gulp.dest(path.dist+"/js/"))
});
//默认命令
gulp.task('default', function () {
	gulp.start(['sass','js']);
});
gulp.watch([
	'public/less/*.less',
	'public/less/*/*.less',
	'public/js/*.js'
], ['default']);